/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package account;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Account implements AccountInterface
{
    private String userName;
    private String password;
    private String firstName;
    private String lastName;
    private String alternativeMail;
    private String securityQn;
    private String qnAnswer;
    private String country;
    private String city;
    private String postalCode;
    private String gender;
    private Date birthday;
    /**
     * constructor to initialize instance variables
     */
    public Account()
    {
        userName = "";
        password = "";
        firstName = "";
        lastName = "";
        alternativeMail = "";
        securityQn = "";
        qnAnswer = "";
        country = "";
        city = "";
        postalCode = "";
        gender = "";
        birthday = null;
    }
    /**
     * This method is used to get user alternative mail
     * @return user alternative mail
     */
    public String getAlternativeMail()
    {
        return alternativeMail;
    }
    /**
     * This method is used to get user birth date
     * @return user birth date
     */
    public Date getBirthday()
    {
        return birthday;
    }
    /**
     * This method is used to get user's city
     * @return user's sity
     */
    public String getCity()
    {
        return city;
    }
    /**
     * This method is used to get user's country
     * @return user's country
     */
    public String getCountry()
    {
        return country;
    }
    /**
     * This method is used to get user first name
     * @return user first name
     */
    public String getFirstName()
    {
        return firstName;
    }
    /**
     * This method is used to get user gebder
     * @return user gebder
     */
    public String getGender()
    {
        return gender;
    }
    /**
     * This method is used to get user last name
     * @return user last name
     */
    public String getLastName()
    {
        return lastName;
    }
    /**
     * This method is used to get user password
     * @return user password
     */
    public String getPassword()
    {
        return password;
    }
    /**
     * This method is used to get postal code
     * @return postal code
     */
    public String getPostalCode()
    {
        return postalCode;
    }
    /**
     * This method is used to get user question answer
     * @return question answer
     */
    public String getQnAnswer()
    {
        return qnAnswer;
    }
    /**
     * This method is used to user security question answer
     * @return security question answer
     */
    public String getSecurityQn()
    {
        return securityQn;
    }
    /**
     * This method is used to get userName
     * @return userName
     */
    public String getUserName()
    {
        return userName;
    }
    /**
     * This method is used to set user alternative mail
     * @param alternativeMail new user alternative mail
     */
    public void setAlternativeMail(String alternativeMail)
    {
        this.alternativeMail = alternativeMail;
    }
    /**
     * This method is used to set user birth date
     * @param birthday new user birth date
     */
    public void setBirthday(Date birthday)
    {
        this.birthday = birthday;
    }
    /**
     * This method is used to set user's city
     * @param city new user's city
     */
    public void setCity(String city)
    {
        this.city = city;
    }
    /**
     * This method is used to set user's country
     * @param country new user's country
     */
    public void setCountry(String country)
    {
        this.country = country;
    }
    /**
     * This method is used to set user first name
     * @param firstName new user first name
     */
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }
    /**
     * This method is used to set user gender
     * @param gender new user gender
     */
    public void setGender(String gender)
    {
        this.gender = gender;
    }
    /**
     * This method is used to set last name
     * @param lastName new user last name
     */
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
    /**
     * This method is used to set user password
     * @param password new user password
     */
    public void setPassword(String password)
    {
        this.password = password;
    }
    /**
     * This method is used to set user postal code
     * @param postalCode new user postal code
     */
    public void setPostalCode(String postalCode)
    {
        this.postalCode = postalCode;
    }
    /**
     * This method is used to set user security question answer
     * @param qnAnswer new security question answer
     */
    public void setQnAnswer(String qnAnswer)
    {
        this.qnAnswer = qnAnswer;
    }
    /**
     * This method is used to set security question
     * @param securityQn new security question
     */
    public void setSecurityQn(String securityQn)
    {
        this.securityQn = securityQn;
    }
    /**
     * This method is used to set userName
     * @param userName new userName
     */
    public void setUserName(String userName)
    {
        this.userName = userName;
    }
    /**
     * This method is used to set all account data
     * this method is used when account data are read from account file
     * @param s array of account data
     */
    public void setAllAccountData(String[] s)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        this.setFirstName(s[0]);
        this.setLastName(s[1]);
        this.setUserName(s[2]);
        this.setPassword(s[3]);
        this.setAlternativeMail(s[4]);
        this.setCity(s[5]);
        this.setCountry(s[6]);
        try
        {
            this.setBirthday(formatter.parse(s[7]));
        }
        catch (ParseException ex)
        {
            Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setGender(s[8]);
        this.setSecurityQn(s[9]);
        this.setQnAnswer(s[10]);
        this.setPostalCode(s[11]);
    }
    /**
     * This method is used to read account file
     * @param user acount file
     * @throws java.io.IOException
     */
    public void readAccountData(File user)throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader(user));
        String data = in.readLine();
        String[]s = data.split(",");
        this.setAllAccountData(s);
    }
    /**
     * This method is used to write account file
     * @param user parent folder which contains this account file (i.e user folder)
     * @throws java.io.IOException
     */
    public void writeAccountData(File user)throws IOException
    {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        File f = new File(user,userName);
        f.createNewFile();
        BufferedWriter out = new BufferedWriter(new FileWriter(f));
        String s = firstName+","+lastName+","+userName+","+password+","+
                alternativeMail+","+city+","+country+","+formatter.format(birthday)+","+
                gender+","+securityQn+","+qnAnswer+","+postalCode;
        out.write(s);
        out.close();
    }

}
